unit uThreadSecurity;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, Vcl.Forms, Vcl.ComCtrls;

type
{$M+}
  TThreadSecurity = class
  private
    //IsThreadActive: Boolean;
  published
    procedure SetPassword;
    procedure Unlock;
    procedure FreezeLock;
    procedure DisablePassword;
    procedure EraseUnit;
  end;
{$M-}

var
  ThreadSecurity: TThreadSecurity;
  EraseThreadMonitor: Boolean;

implementation

uses
  uFrmAction, uFrmSecurity, uTaskFile;

procedure TThreadSecurity.SetPassword;
var
  strPassword: string;
  bfrPassword: TBytes;
  i: Integer;
begin
  try
    if TaskFile.ERR then
      Exit;
    FrmAction.redtHDD.Lines.Add('SECURITY SET PASSWORD');
    FrmAction.redtHDD.Lines.Add('--------------------------------');
    SetLength(bfrPassword, 512);
    if not TaskFile.SetPWD then
      Exit;
    if FrmSecurity.chkMaster.Checked then
    begin
      bfrPassword[1] := 1;
      FrmAction.redtHDD.Lines.Add(' - Set Master Password');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Set User Password');
    if FrmSecurity.chkMaximum.Checked then
    begin
      bfrPassword[0] := 1;
      FrmAction.redtHDD.Lines.Add(' - Security Level Maximum');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Security Level High');
    strPassword := FrmSecurity.edtPassWord.Text;
    for i := 1 to Length(strPassword) do
      bfrPassword[i + 1] := Byte(strPassword[i]);
    if not TaskFile.WriteDataFromBuffer(bfrPassword) then
      Exit;
    if not TaskFile.WaitNotBSY() then
      Exit;
    if TaskFile.ERR then
      FrmAction.redtHDD.Lines.Add(' - Error' + #13#10)
    else
      FrmAction.redtHDD.Lines.Add(' - Success' + #13#10);
  finally
    SetLength(bfrPassword, 0);
  end;
end;

procedure TThreadSecurity.Unlock;
var
  strPassword: string;
  bfrPassword: TBytes;
  i: Integer;
begin
  try
    if TaskFile.ERR then
      Exit;
    FrmAction.redtHDD.Lines.Add('SECURITY UNLOCK');
    FrmAction.redtHDD.Lines.Add('--------------------------------');
    SetLength(bfrPassword, 512);
    if not TaskFile.UnlockUnit then
      Exit;
    if FrmSecurity.chkMaster.Checked then
    begin
      bfrPassword[1] := 1;
      FrmAction.redtHDD.Lines.Add(' - Compare Master Password');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Compare User Password');
    if FrmSecurity.chkMaximum.Checked then
    begin
      bfrPassword[0] := 1;
      FrmAction.redtHDD.Lines.Add(' - Security Level Maximum');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Security Level High');
    strPassword := FrmSecurity.edtPassWord.Text;
    for i := 1 to Length(strPassword) do
      bfrPassword[i + 1] := Byte(strPassword[i]);
    if not TaskFile.WriteDataFromBuffer(bfrPassword) then
      Exit;
    if not TaskFile.WaitNotBSY() then
      Exit;
    if TaskFile.ERR then
      FrmAction.redtHDD.Lines.Add(' - Error' + #13#10)
    else
      FrmAction.redtHDD.Lines.Add(' - Success' + #13#10);
  finally
    SetLength(bfrPassword, 0);
  end;
end;

procedure TThreadSecurity.FreezeLock;
begin
  try
    if TaskFile.ERR then
      Exit;
    FrmAction.redtHDD.Lines.Add('SECURITY FREEZE LOCK');
    FrmAction.redtHDD.Lines.Add('--------------------------------');
    if not TaskFile.FreezeLock then
      FrmAction.redtHDD.Lines.Add(' - Error' + #13#10)
    else
      FrmAction.redtHDD.Lines.Add(' - Success' + #13#10);
  finally
  end;
end;

procedure TThreadSecurity.DisablePassword;
var
  strPassword: string;
  bfrPassword: TBytes;
  i: Integer;
begin
  try
    if TaskFile.ERR then
      Exit;
    FrmAction.redtHDD.Lines.Add('SECURITY DISABLE PASSWORD');
    FrmAction.redtHDD.Lines.Add('--------------------------------');
    SetLength(bfrPassword, 512);
    if not TaskFile.DisablePWD then
      Exit;
    if FrmSecurity.chkMaster.Checked then
    begin
      bfrPassword[1] := 1;
      FrmAction.redtHDD.Lines.Add(' - Compare Master Password');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Compare User Password');
    if FrmSecurity.chkMaximum.Checked then
    begin
      bfrPassword[0] := 1;
      FrmAction.redtHDD.Lines.Add(' - Security Level Maximum');
    end
    else
      FrmAction.redtHDD.Lines.Add(' - Security Level High');
    strPassword := FrmSecurity.edtPassWord.Text;
    for i := 1 to Length(strPassword) do
      bfrPassword[i + 1] := Byte(strPassword[i]);
    if not TaskFile.WriteDataFromBuffer(bfrPassword) then
      Exit;
    if not TaskFile.WaitNotBSY() then
      Exit;
    if TaskFile.ERR then
      FrmAction.redtHDD.Lines.Add(' - Error' + #13#10)
    else
      FrmAction.redtHDD.Lines.Add(' - Success' + #13#10);
  finally
    SetLength(bfrPassword, 0);
  end;
end;

procedure TThreadSecurity.EraseUnit;
var
  bfrIdentify, bfrPassword: TBytes;
  uFlag: Word;
begin
  try
    if TaskFile.ERR then
      Exit;
    FrmAction.redtHDD.Lines.Add('SECURITY ERASE UNIT');
    FrmAction.redtHDD.Lines.Add('--------------------------------');
    SetLength(bfrIdentify, 512);
    SetLength(bfrPassword, 512);
    if not TaskFile.GetIdentify(bfrIdentify) then
      Exit;
    uFlag := bfrIdentify[256] + bfrIdentify[257] shl 8;
    if uFlag and 1 = 1 then
    begin
      if uFlag and 2 = 2 then
      begin

      end
      else
      begin
        if not TaskFile.SetPWD then
          Exit;
        if not TaskFile.WriteDataFromBuffer(bfrPassword) then
          Exit;
        if not TaskFile.WaitNotBSY() then
          Exit;
        FrmAction.redtHDD.Lines.Add(' - Set Blank User Password');
      end;
      if not TaskFile.ErasePrep then
        Exit;
      FrmAction.redtHDD.Lines.Add(' - SECURITY ERASE PREPARE');
      if uFlag and $20 = $20 then
      begin
        bfrPassword[1] := 2;
        FrmAction.redtHDD.Lines.Add(' - Enhanced Erase');
      end
      else
        FrmAction.redtHDD.Lines.Add(' - Normal Erase');
      if not TaskFile.EraseUnit then
        Exit;
      if not TaskFile.WriteDataFromBuffer(bfrPassword) then
        Exit;
      if TaskFile.ERR then
        FrmAction.redtHDD.Lines.Add(' - Error' + #13#10)
      else
      begin
        FrmAction.redtHDD.Lines.Add(' - Success' + #13#10);
        FrmAction.redtHDD.Lines.Add('Please Wait For The Drive To Be Ready' + #13#10);
        if EraseThreadMonitor then
          while TaskFile.BSY do
      end;
    end;
  finally
    SetLength(bfrIdentify, 0);
    SetLength(bfrPassword, 0);
  end;
end;

end.

